<!--
 * @Author: [you name]
 * @Date: 2021-10-26 14:29:05
 * @LastEditors: [you name]
 * @LastEditTime: 2021-11-06 01:24:02
 * @Description: 
-->
<template>
  <div class="customer">
    <!-- 新增按钮 -->
    <div class="btns">
      <el-form
        :inline="true"
        :model="queryParams"
        class="demo-form-inline"
        size="small"
      >
        <el-form-item>
          <el-button type="primary" @click="toAddHandler()">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="queryParams.username"
            clearable
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="queryParams.status"
            clearable
            placeholder="请选择状态"
          >
            <el-option label="正常" value="正常"></el-option>
            <el-option label="禁用" value="禁用"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="pageQueryEmployee">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格区域 -->
    <el-table :data="tableData.list">
      <el-table-column prop="id" label="员工编号"></el-table-column>
      <!-- <el-table-column type="id" label="编号"></el-table-column> -->

      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column prop="realname" label="姓名"></el-table-column>
      <el-table-column prop="telephone" label="联系方式"></el-table-column>
      <el-table-column prop="gender" label="性别"></el-table-column>
      <el-table-column label="状态">
        <template v-slot="scope">
          <el-tag v-if="scope.row.status == '正常'" type="success" size="mini"
            >{{ scope.row.status }}
          </el-tag>
          <el-tag v-else type="danger" size="mini"
            >{{ scope.row.status }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" v-slot="scope">
        <el-button
          type="text"
          @click="toEditHandler(scope.row)"
          style="color: #30b08f"
          >编辑</el-button
        >
        <el-button
          type="text"
          @click="toDeleteHandler(scope.row.id)"
          style="color: #e65d6e"
          >删除</el-button
        >
        <el-button
          type="text"
          @click="toDetailHandler(scope.row.id)"
          style="color: #3a71a8"
          >详情</el-button
        >
      </el-table-column>
    </el-table>

    <!-- 分页插件区域 -->
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="tableData.page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="tableData.size"
      layout="total, sizes, prev, pager, next"
      :total="tableData.total"
    >
    </el-pagination>

    <!-- 新增顾客模态框 -->
    <el-dialog title="添加员工信息" :visible.sync="visible">
      <el-form :model="custom" :rules="rules" ref="custForm">
        <el-form-item
          label="用户名"
          :label-width="formLabelWidth"
          prop="username"
        >
          <el-input
            v-model="custom.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="姓名"
          :label-width="formLabelWidth"
          prop="realname"
        >
          <el-input
            v-model="custom.realname"
            placeholder="请输入姓名"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="手机号"
          :label-width="formLabelWidth"
          prop="telephone"
        >
          <el-input
            v-model="custom.telephone"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="密码"
          :label-width="formLabelWidth"
          prop="password"
        >
          <el-input
            v-model="custom.password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth" prop="gender">
          <el-radio v-model="custom.gender" label="male">男</el-radio>
          <el-radio v-model="custom.gender" label="female">女</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="toSaveHandler">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import request from "@/utils/request";
import qs from "qs";
import _ from "lodash";
export default {
  data() {
    return {
      tableData: {},
      queryParams: {
        page: 1,
        pageSize: 10,
        roleId: 1,
      },
      visible: false,
      formLabelWidth: "80px",
      custom: {},
      title: "",
      rules: {
        username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        realname: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        telephone: [
          { required: true, message: "请输入联系方式", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        gender: [{ required: true, message: "请选择性别", trigger: "change" }],
      },
    };
  },
  methods: {
    // 1.分页查询顾客信息
    pageQueryEmployee() {
      // 过滤查询参数，将空字符串的键值对删除掉
      for (let key in this.queryParams) {
        if (this.queryParams[key] == "") {
          delete this.queryParams[key];
        }
      }
      request
        .get("/baseUser/pageQuery", {
          params: this.queryParams,
        })
        .then((res) => {
          console.log(res.data);
          this.tableData = res.data;
        });
    },
    // 编辑按钮
    toEditHandler(row) {
      this.custom = _.clone(row); //将当前行深拷贝后，显示到表单
      this.visible = true; //打开模态框
    },
    // 删除按钮
    toDeleteHandler(id) {
      // 点击删除按钮弹出提示框
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        //   根据路由/baseUser/deleteById 的id删除信息
        request
          .get("/baseUser/deleteById", {
            params: {
              id,
            },
          })
          .then((res) => {
            //   重载数据
            this.pageQueryEmployee();
            this.$message({
              message: res.message,
              type: "success",
            });
          });
      });
    },
    // 详情按钮
    toDetailHandler(id) {
      // console.log(id)
      this.$router.push({
        path: 'details',
        query: {
          id,
        }
      })
    },
    // 分页size-change事件
    handleSizeChange(pageSize) {
      // 点击分页容量，将最新的pageSize设置到数据模板
      this.queryParams.pageSize = pageSize;
      // 重新调用分页查询的方法，做数据重载
      this.pageQueryEmployee();
    },
    // 分页current-change事件
    handleCurrentChange(page) {
      this.queryParams.page = page;
      this.pageQueryEmployee();
    },
    // 添加按钮点击事件
    toAddHandler() {
      this.custom = {}; //模态框打开之前，置空表单
      this.title = "添加顾客"; //设置模态框的标题
      this.visible = true; //打开模态框
    },
    // 模态框确定按钮点击事件
    toSaveHandler() {
      // 模态框验证
      this.$refs["custForm"].validate((valid) => {
        if (valid) {
          // 角色id是6
          this.custom.roleId = 1;
          const data = qs.stringify(this.custom);
          request.post("/baseUser/addUserWidthRole", data).then((res) => {
            this.pageQueryEmployee();
            this.visible = false;
            this.$message.success(res.message);
          });
        }else{
            return false;
        }
      });
    },
  },

  created() {
    this.pageQueryEmployee(); //调用分页查询顾客信息
  },
};
</script>
<style scoped>
</style>